﻿using ManagementApi.Models.CheckIn;
using ManagementApi.Mvc;
using Modules.CheckIn;
using Modules.CheckIn.Entitys;
using Modules.Project;
using Modules.Project.Entitys;
using PmSoft;

namespace ManagementApi.Services
{
    /// <summary>
    /// 考勤统计
    /// </summary>
    [UseDI(ServiceLifetime.Scoped)]
    public class PublicCheckinService
    {
        private readonly CheckInDetailService checkInDetailService;
        private readonly ProjectGroupMemberService projectGroupMemberService;
        private readonly ProjectGroupService projectGroupService;

        public PublicCheckinService(CheckInDetailService checkInDetailService,
                                 ProjectGroupMemberService projectGroupMemberService,
                                  ProjectGroupService projectGroupService)
        {
            this.checkInDetailService = checkInDetailService;
            this.projectGroupService = projectGroupService;
            this.projectGroupMemberService = projectGroupMemberService;
        }

        /// <summary>
        /// 项目考勤统计-数量统计
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public ProjectCheckinCountDto GetProjectCheckinCount(ProjectCheckinCountQuery query)
        {
            ProjectCheckinCountDto dto = new ProjectCheckinCountDto();
            dto.ProjectPersonCount = projectGroupMemberService.GetProjectPersonCount(query);
            dto.CheckinPersonCount = checkInDetailService.GetCheckInDetailCount(query);
            dto.AbsenceFromDutyPersonCount = dto.ProjectPersonCount - dto.CheckinPersonCount;
            dto.GroupCount = projectGroupService.GetProjectGroupCount(query);
            if (dto.CheckinPersonCount == 0 || dto.ProjectPersonCount == 0)
                dto.Attendance = 0;
            else
                dto.Attendance = Convert.ToDecimal(dto.CheckinPersonCount) / Convert.ToDecimal(dto.ProjectPersonCount);
            if (dto.AbsenceFromDutyPersonCount == 0 || dto.ProjectPersonCount == 0)
                dto.AbsenceRate = 0;
            else
                dto.AbsenceRate = Convert.ToDecimal(dto.AbsenceFromDutyPersonCount) / Convert.ToDecimal(dto.ProjectPersonCount);
            return dto;
        }

        /// <summary>
        /// 项目考勤统计-班组考勤分析
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public ProjectGroupCheckinAnalysisDto GetProjectGroupCheckinAnalysis(ProjectCheckinCountQuery query)
        {
            ProjectGroupCheckinAnalysisDto dto = new ProjectGroupCheckinAnalysisDto();
            List<ProjectGroup> projectGroups = projectGroupService.GetProjectGroupsByProjectId(query).ToList();
            List<PersonCount> groupPersonCounts = projectGroupMemberService.GetPersonCountsByProjectId(query).ToList();
            var checkinPersonCounts = checkInDetailService.GetPersonCountsByProjectId(query);
            foreach (var item in projectGroups)
            {
                dto.GroupNames.Add(item.Name);
                var checkinCount = checkinPersonCounts.FirstOrDefault(a => a.GroupId == item.GroupId)?.Count ?? 0;
                dto.CheckinPersonCount.Add(checkinCount);
                //当前班组总人数
                var groupCount = groupPersonCounts.FirstOrDefault(a => a.GroupId == item.GroupId)?.Count ?? 0;
                dto.AbsenceFromDutyPersonCount.Add(groupCount - checkinCount);
                decimal onDutyRate = 0;
                if (checkinCount == 0 || groupCount == 0)
                    onDutyRate = 0;
                else
                    onDutyRate = Convert.ToDecimal(checkinCount) / Convert.ToDecimal(groupCount);
                dto.OnDutyRate.Add(onDutyRate);
                if (groupCount == 0)
                    dto.AbsenceFromDutyRate.Add(0);
                else
                    dto.AbsenceFromDutyRate.Add(1 - onDutyRate);
            }
            return dto;
        }
    }
}
